<template>
	<view class="course-page-box">
		<z-paging ref="paging" v-model="courseList" @query="getAttendanceList" :safe-area-inset-bottom="true"
			:auto="false">
			<view slot="top">
				<view class="tab-box">
					<u-tabs :list="tabsList" :current="tabsIndex" @click="tabsChange" lineWidth="20" lineColor="#0079FF"
						:scrollable="false">
					</u-tabs>
				</view>
				<view class="data-box">
					<view class="data-item" @click="showYearMonth = true">
						<view class="data-label">{{year||"选择年份"}}</view>
						<image src="@/static/index/arrow-down.png" class="down-icon"></image>
					</view>
					<view class="data-item" @click="showYearMonth = true">
						<view class="data-label">{{month||"选择月份"}}</view>
						<image src="@/static/index/arrow-down.png" class="down-icon"></image>
					</view>
				</view>
			</view>
			<view class="course-list">
				<view class="course-item" v-for="( item, index ) in courseList" :key="index"
					@click="toAttendancePage(item)">
					<view class="item-top">
						<view class="course-title">{{item.course.title}}</view>
						<view class="course-time">{{item.date}}</view>
					</view>
					<view class="item-center" v-if="tabsIndex==2">
						<view class="tag-box normal" v-if="item.is_sign==='2'">正常</view>
						<view class="tag-box  late" v-if="item.is_sign==='3'">迟到</view>
						<view class="tag-box  absent" v-if="item.is_sign==='1'">缺勤</view>
						<view class="tag-box  success" v-if="item.image">已提交</view>
						<view class="tag-box  error" v-else>未提交</view>
					</view>
					<view class="item-bottom">
						<view class="bottom-info">
							<text class="label">学校：</text>
							<text class="value">{{item.school.title}}</text>
						</view>
						<view class="bottom-info">
							<text class="label">班级：</text>
							<text class="value">{{item.banji.title}}</text>
						</view>
						<view class="bottom-info">
							<text class="label">学期：</text>
							<text class="value">{{item.term.title}}</text>
						</view>
						<view class="bottom-info">
							<text class="label">班级人数：</text>
							<text class="value">{{item.banji.student_num}}人</text>
						</view>
						<view class="bottom-info">
							<text class="label">上课地点：</text>
							<text class="value">{{item.course.place}}</text>
						</view>
						<view class="bottom-info">
							<text class="label">上课时间：</text>
							<text class="value">第{{item.schedule.num}}节({{item.schedule.class_time}})</text>
						</view>
					</view>
				</view>
			</view>
		</z-paging>
		<u-datetime-picker :show="showYearMonth" v-model="pickerDate" mode="year-month" closeOnClickOverlay
			@confirm="confirmPickerDate" @change="changePickerDate" @cancel="showYearMonth=false"></u-datetime-picker>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				tabsList: [],
				tabsIndex: 0,
				year: '', // 当前年份
				month: '', // 当前月份
				pickerDate: new Date(),
				showYearMonth: false, // 是否显示年月选择器
				courseList: [], // 课程列表
			}
		},
		onShow(options) {
			this.refreshList();
		},
		methods: {
			getTabsListCount() {
				// 获取各个状态的考勤数量
				this.$Api.user.getAttendanceCount({
					year: this.year,
					month: this.month
				}).then(res => {
					this.tabsList = [{
						name: `未开始(${res.data[0] || 0})`,
						status: '1',
					}, {
						name: `进行中(${res.data[1] || 0})`,
						status: '2',
					}, {
						name: `已结束(${res.data[2] || 0})`,
						status: '3',
					}];
				});
			},
			toAttendancePage(item) {
				// 跳转到考勤详情页面
				if (this.tabsIndex == 0) {
					// this.$Toast('未开始的课程无法考勤')
					this.$Tips('/pages/toAttendance/toAttendance?courseId=' + item.id + '&schoolId=' + item.school.id)
				} else if (this.tabsIndex == 1) {
					this.$Tips('/pages/toAttendance/toAttendance?courseId=' + item.id + '&schoolId=' + item.school.id)
				} else if (this.tabsIndex == 2) {
					this.$Tips('/pages/attendanceDetail/attendanceDetail?courseId=' + item.id + '&schoolId=' + item.school
						.id)
				}
			},
			changePickerDate(e) {
				this.pickerDate = e.value; // 更新选择的日期
			},
			confirmPickerDate(e) {
				this.showYearMonth = false; // 关闭选择器
				this.year = new Date(e.value).getFullYear()
				this.month = (new Date(e.value).getMonth() + 1).toString().padStart(2, '0'); // 确保月份是两位数
				this.refreshList();
			},
			tabsChange(item) {
				this.courseList = []
				this.tabsIndex = item.index
				this.refreshList()
			},
			refreshList() {
				// this.$refs.paging.scrollToTop()
				this.$refs.paging.refresh();
				this.getTabsListCount();
			},
			getAttendanceList(pageNo, pageSize) {
				this.$Api.user.getAttendanceList({
					page: pageNo,
					limit: pageSize,
					year: this.year,
					month: this.month,
					sort: this.tabsIndex == 2 ? 'desc' : 'asc',
					type: this.tabsList[this.tabsIndex].status
				}).then(res => {
					this.$refs.paging.complete(res.data.data);
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.course-page-box {
		min-height: 100vh;
		background: #f5f6fa;
	}

	.tab-box {
		min-height: 86rpx;
		margin: 24rpx;
	}

	.data-box {
		display: flex;
		justify-content: space-between;
		margin: 0 20rpx 20rpx;

		.data-item {
			width: 332rpx;
			height: 76rpx;
			background-color: #fff;
			border-radius: 12rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 0 24rpx;
			box-sizing: border-box;

			.data-label {
				font-size: 26rpx;
				color: #606060;
			}

			.down-icon {
				width: 16rpx;
				height: 12rpx;
			}
		}
	}

	.course-list {
		padding: 20rpx;

		.course-item {
			background: #fff;
			box-shadow: 0rpx 4rpx 8rpx 0rpx rgba(0, 0, 0, 0.01);
			border-radius: 24rpx;
			padding: 32rpx;
			margin-bottom: 20rpx;

			.item-top {
				display: flex;
				justify-content: space-between;
				padding-bottom: 10rpx;

				.course-title {
					font-size: 30rpx;
					color: #2F1F1A;
					line-height: 42rpx;
					font-weight: bold;
				}

				.course-time {
					font-size: 24rpx;
					color: #333333;
				}
			}

			.item-center {
				display: flex;
				padding-bottom: 10rpx;

				.tag-box {
					position: relative;
					width: 111rpx;
					height: 48rpx;
					text-align: center;
					line-height: 48rpx;
					border-radius: 6rpx;
					margin-right: 20rpx;
					font-size: 22rpx;
				}

				.normal {
					background: #ebf7ff;
					border: 1rpx solid #0095F8;
					color: #0095F8;
				}

				.late {
					background: #FFF3ED;
					border: 1rpx solid #FE4F04;
					color: #FE4F04;
				}

				.absent {
					background: #FFF9ED;
					border: 1rpx solid #F8A500;
					color: #F8A500;
				}

				.success {
					background: #F3FFED;
					border: 1rpx solid #4ED142;
					color: #53D347;
				}

				.error {
					background: #FDEDED;
					border: 1rpx solid #FE4F04;
					color: #FE4F04;
				}
			}

			.item-bottom {
				font-size: 26rpx;
				color: #999999;
				padding-top: 30rpx;
				border-top: 2rpx solid #EEEEEE;

				.bottom-info {
					margin-bottom: 16rpx;

					.value {
						margin-left: 10rpx;
						color: #333333;
					}
				}
			}
		}
	}
</style>